// xc_lshop/shop/user/rank.js
var app = getApp()
var actbill = require('../../../xc/model/sysdata')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    type:3,
    nav:[
      {type:1,"name":"总消费金额"},
      {type:2,"name":"购买值"},
      {type:3,"name":"奖励"},
    ],
    basicurl: app.globalData.basicurl,
    CustomBar: app.globalData.CustomBar,
    year:new Date().getFullYear(),
    month:new Date().getMonth() + 1,
    xcmodel:null,
    week:0
  },

  choosetime(){
    this.selectComponent("#datetimepicker").init(this.data.year,this.data.month);
  },
  xcloaddata: function (options, config) {
    if (config.task){
      console.log("config.task")
      console.log(config.task.prizes)
      var name="nav["+ 1 +"].name"
      var goldcoin=config.task.goldcoin?config.task.goldcoin:"金币"
      this.setData({
        goldcoin:goldcoin,
        taskconfig:config.task,
        [name]:config.task.taskname
      })
  
    }
    this.getpagelist(this,1)
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {}
      }
      strwhere["type"] = this.data.type
      strwhere["week"] = this.data.week

      that.setData({
        modellist: [],
        isempty: false
      });

    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      console.log("result")
      console.log(result)
      var updatemdoel = {};
      if(result["obj"]["xcmodel"]){
        that.setData({
         xcmodel:result["obj"]["xcmodel"]
        }) 
      }
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (result["obj"]["modellist"].length < 1) {
          updatemdoel["isempty"] = true
        } else if (rest == 1) {
          // var mo=result["obj"]["modellist"]
          // mo=mo.concat(result["obj"]["modellist"]);
          // mo=mo.concat(result["obj"]["modellist"]);

          // mo=mo.concat(result["obj"]["modellist"]);
          // mo=mo.concat(result["obj"]["modellist"]);
          // mo=mo.concat(result["obj"]["modellist"]);
          // mo=mo.concat(result["obj"]["modellist"]);
          // mo=mo.concat(result["obj"]["modellist"]);

          // updatemdoel["modellist"] =mo;

          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
           
         
         
          }
        }
      }
      // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
      // wx.stopPullDownRefresh();
      that.setData(updatemdoel);
    }, function () {
  
    });
  },
  choose(e){
    this.setData({
      'year':e.detail.year,
      'month':e.detail.month,
    })
    
    this.getpagelist(this,1)

  },
  onClick(e){
    console.log(e)
    var type=e.currentTarget.dataset.type;
    this.setData({
      type:type
    })
    this.getpagelist(this,1)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getpagelist(this,-1)

  },
  preweek(){
    this.setData({
      week:this.data.week + 1
    })
    this.gettime(this.data.week)
    this.getpagelist(this,1)
  },
  nextweek(){
    this.setData({
      week:this.data.week - 1
    })
    this.gettime(this.data.week)

    this.getpagelist(this,1)

  },
  gettime(week){
    console.log("week")
    console.log(week)
    var time=(new Date).getTime() - 24 *60*60* 1000 * week * 7
    console.log("time")

    console.log(time)
    this.GetMonday(time)
    this.GetSunday(time)
  },
  GetMonday(time) {
    var dd = new Date(time)
    console.log("dddddddddddddddd")
    console.log(dd)
    var week = dd.getDay(); //获取时间的星期数
    var minus = week ? week - 1 : 6;
    dd.setDate(dd.getDate() - minus); //获取周一日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
    var stardate= y + "-" + m + "-" + d;
    this.setData({
      stardate:stardate
    })
  },
  GetSunday(time) {
    var dd = new Date(time)
    var week = dd.getDay(); //获取时间的星期数
    var minus = week ? week - 1 : 6;
    dd.setDate(dd.getDate() - minus);//获取周一日期
    dd.setDate(dd.getDate()+6)//加6得到周日日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
    var enddate= y + "-" + m + "-" + d;
    this.setData({
      enddate:enddate
    })
  }
})